﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using facebook.Components;
using facebook.desktop;
using facebook.Forms;
using facebook.Schema;
using facebook.Types;
using facebook.Utility;
using Microsoft.Xml;

namespace PhotoMonkey
{
    public partial class MainGUI : Form
    {
        static user ActiveUser;
        user selectedfriend;
        public MainGUI()
        {
            InitializeComponent();
           // facebookService = new FacebookService();
            facebookService.IsDesktopApplication = true;

        }
     public    void Putevents()
        {
            try
            {


                IList<facebookevent> eventcol = facebookService.API.events.get();
              //  eventList = new EventList();
                
                eventList.FacebookEvents = new System.Collections.ObjectModel.Collection<facebookevent>();
                if ((eventcol!=null )&&(eventList.FacebookEvents!=null) )
                {
                    
                   // eventlst.FacebookEvents = ;
                    
                    foreach (facebookevent evnt in eventcol)
                    {
                      
                        
                        if (evnt != null)
                        {

                            eventList.FacebookEvents.Add(evnt);
                        }
                    }

                }
               


            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);
                

            }


        }
        public void fillProfile(user  user)
        {
            try
            {


                if(user !=null)
                {
                    this.profile.User = user;
                    this.lblStatusSet.Text = user.first_name;

                }
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }


        }
        public void GetAlbums(user  user)
        {
            try
            {

             //   if(user !=null)
                {
                
                      photoAlbum.Albums= facebookService.photos.getAlbums(user.uid);
                     
                        
                }

                   
                   
                    
                 


                
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }


        }
        private void MainGUI_Load(object sender, EventArgs e)
        {
            try
            {


                 facebookService.ConnectToFacebook();
                 this.NotIcon.Icon = this.Icon;
                 this.NotIcon.Text = Application.ProductName + " - " + Application.ProductVersion;
                
                tabControl.Select();
                this.Text = Application.ProductName + " - "+ Application.ProductVersion;
                //if (facebookService.SessionExpires == false )
                {
                    friendList.Friends = facebookService.friends.getUserObjects();
                    GetAlbums(facebookService.API.users.getInfo());
                    
                    //photoAlbum.Albums = facebookService.photos.getAlbums();
                    fillProfile(facebookService.users.getInfo());

                    Putevents();
                    this.notificationsList1.FacebookService = this.facebookService;
                    


                }
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }
        public string FBProfileStatus
        {
            get { return txtStatus.Text; }
            set { txtStatus.Text = value; }
        }
        private void mnuProfile_Click(object sender, EventArgs e)
        {
            try
            {
                Formprofile frmprof;
                if(selectedfriend !=null )
                {
                    frmprof = new Formprofile(selectedfriend);
                    frmprof.Show();


                }



            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void friendList_FriendSelected(object sender, FriendSelectedEventArgs e)
        {
            try
            {
                if(e !=null )
                {
                    selectedfriend = e.User;

                }



            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void mnuPhotos_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectedfriend != null)
                {
                    FormPhotoAlbums albums = new FormPhotoAlbums(facebookService,selectedfriend);
                    albums.Show();
                }



            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void tabControl_Selected(object sender, TabControlEventArgs e)
        {
            try
            {
                if(e.TabPage.Tag=="profile")
                {
                    fillProfile(facebookService.API.users.getInfo());

                }
                else if(e.TabPage.Tag=="events")
                {
                    Putevents();

                }
                else if (e.TabPage.Tag == "photos")
                {
                    GetAlbums(facebookService.API.users.getInfo());
                    photoAlbum.Select();
                    

                }


            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }
      
       
        private void MainGUI_Resize(object sender, EventArgs e)
        {
            
        }

        private void btnPublish_Click(object sender, EventArgs e)
        {
            try
            {


                if (facebookService.users.hasAppPermission(Enums.Extended_Permissions.status_update) != true)
                {
                    facebookService.GetExtendedPermission(Enums.Extended_Permissions.status_update);
                    facebookService.API.users.setStatus(txtStatus.Text);
                    this.txtStatus.Text = "";
                    fillProfile(facebookService.API.users.getInfo());
                
                }
                else
                {
                    facebookService.API.users.setStatus(txtStatus.Text);
                    this.txtStatus.Text = "";
                    fillProfile(facebookService.API.users.getInfo());
                }
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {

                Application.Exit();

            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
            
        }

        private void btnShowHideGui_Click(object sender, EventArgs e)
        {
            if ((this.WindowState == FormWindowState.Minimized) && (this.ShowInTaskbar == false))
            {
                this.ShowInTaskbar = true;

                this.Show();
                this.WindowState = FormWindowState.Normal;

            }
            else
            {
                this.ShowInTaskbar = false;
                this.WindowState = FormWindowState.Minimized;


            }

        }

        private void NotIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                facebookService.LogOff();
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                facebookService.ConnectToFacebook();
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        private void eventList_EventSelected(object sender, EventSelectedEventArgs e)
        {
            try
            {
             if(e.FacebookEvent !=null)
                {  
                    lstInvites.FacebookEvent = e.FacebookEvent;
                   ;
                 event_members eventmemb = facebookService.API.events.getMembers(e.FacebookEvent.eid);
                 if ((eventmemb != null) &&(lstInvites.EventMembers !=null))
                 {  
                     lstInvites.EventMembers.Add(eventmemb);
                     
                 }
                    

                }
            }
            catch (Exception ex)
            {
                Program.ErrorReport(ex);

            }
        }

        

      
    }
}
